import {
  defineStore
} from "pinia";
import {
  getSwiperImages,
  getShopDetail,
  getShops
} from "../api/index.js";

import {
  shopList,
  mune
} from "../api/data.js"

export const useShopStore = defineStore("shop", {
  state: () => ({
    shops: [],
    cartItems: [],
    swiper: [],
    mune: [],
  }),
  actions: {
    addToCart(item) {
      const existingItem = this.cartItems.find((i) => i.id === item.id);
      if (existingItem) {
        existingItem.quantity++;
      } else {
        this.cartItems.push({
          ...item,
          quantity: 1,
        });
      }
    },
    removeFromCart(itemId) {
      this.cartItems = this.cartItems.filter((item) => item.id !== itemId);
    },
    updateQuantity(itemId, quantity) {
      const item = this.cartItems.find((i) => i.id === itemId);
      if (item) {
        item.quantity = quantity;
      }
    },
    clearCart() {
      this.cartItems = [];
    },
    setCurrentShop(shop) {
      this.currentShop = shop;
    },
    async getShopById(id) {
      return await getShopDetail(id)
    },
    async getAllShops(options) {
      this.shops = await getShops(options);
      return this.shops
    },
    getMune(id) {
      return getShopDetail(id);
    },
    async getSwipImages() {
      this.swiper = await getSwiperImages();
      return this.swiper;
    },
  },
  getters: {
    cartCount: (state) => {
      return state.cartItems.reduce((total, item) => total + item.quantity, 0);
    },
    cartTotal: (state) => {
      return state.cartItems.reduce(
        (total, item) => total + item.price * item.quantity,
        0
      );
    },
  },
});